import 'whatwg-fetch';

import * as queryString from 'querystring';
let $script = require('scriptjs');
declare let window: any;
const systemCodeZipMap = {
  D2C: 'H4sIAAAAAAAAA0sxSg4uyS9KBQDJOKt4CAAAAA==',
  D2P: 'H4sIAAAAAAAAA0sxKgAAo5/qDQMAAAA=',
  EHOME: 'H4sIAAAAAAAAA0vNyM9NBQDs3GLHBQAAAA=='
};
const SystemCodeZip = systemCodeZipMap.D2C;
/**
 * 获取对应环境的建站
 */
export const getEnvXSite = (EnvID = window.Server.getEnvId()) => {
  switch (EnvID) {
    case '0':
      return '//x-site.1000.com';
    case '1':
      return '//172.19.66.62:3000';
    case '2':
      return '//172.21.4.7:3000';
    case '3':
      return '//x-site.qianmi3.com:3000';
    case '4':
      return '//172.19.68.43:3000';
    case '5':
      return '//172.21.33.3:3000';
    case '6':
      return '//172.19.67.113:3000';
    case '8':
      return '//172.19.71.172:3000';
    default:
      return '//x-site.1000.com';
  }
};

export const getAdminD2cApiHost = (EnvID = window.Server.getEnvId()) => {
  switch (EnvID) {
    case '0':
      return '//d2c-api.1000.com';
    case '1':
      return '//172.21.34.133:8080';
    case '2':
      return '//172.19.67.200:8080';
    case '3':
      return '';
    case '4':
      return '//172.19.68.30:8080';
    case '5':
      return '//172.21.33.57:8080';
    case '6':
      return '//172.21.4.229:8080';
    default:
      return '//d2c-api.1000.com';
  }
};

export const getServerUrl = (EnvID = window.Server.getEnvId()) => {
  switch (EnvID) {
    case '0':
      return 'http://img.1000.com/qm-a-img/prod/';
    case '1':
      return 'http://img.1000.com/qm-a-img/test1/';
    case '2':
      return 'http://img.1000.com/qm-a-img/test2/';
    case '3':
      return '';
    case '4':
      return 'http://img.1000.com/qm-a-img/test4/';
    case '5':
      return 'http://img.1000.com/qm-a-img/test5/';
    case '6': //性能环境
      return 'http://img.1000.com/qm-a-img/test0/';
    case '8':
      return 'http://img.1000.com/qm-a-img/testx/';
    default:
      return '';
  }
};

export const getSessionId = (sessionId?: string) => {
  if (window.Session) {
    sessionId = window.Session.getSession();
  }
  return sessionId;
};

//封装给建站openApi的http请求方法
let sessionId = '';
if (window.Session) {
  sessionId = window.Session.getSession();
}

export const $http = {
  get: function(url, params = {}) {
    params = Object.assign({}, params, { sc: SystemCodeZip });
    params = queryString.stringify(params);
    return new Promise((resolve, reject) => {
      fetch(getEnvXSite() + url + '?' + params, {
        method: 'GET',
        mode: 'cors',
        credentials: 'include',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
          Authorization: sessionId
        }
      }).then(
        response => {
          if (response.status !== 200) {
            reject(response);
          } else {
            response.json().then(result => {
              resolve(result);
            });
          }
        },
        err => {
          reject(err);
        }
      );
    });
  },
  post: function(url, params = {}) {
    params = Object.assign({}, params, { sc: SystemCodeZip });
    return new Promise((resolve, reject) => {
      fetch(getEnvXSite() + url, {
        method: 'POST',
        mode: 'cors',
        credentials: 'include',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
          Authorization: sessionId
        },
        body: JSON.stringify(params)
      }).then(
        response => {
          if (response.status !== 200) {
            reject(response);
          } else {
            response.json().then(result => {
              resolve(result);
            });
          }
        },
        err => {
          reject(err);
        }
      );
    });
  }
};
export const loadJS = path => {
  return new Promise((resolve, reject) => {
    $script(path, () => {
      resolve();
    });
  });
};
